package config

import (
	"encoding/base64"
	"encoding/json"
	"fmt"

	"github.com/tidwall/gjson"
	"github.com/xuelang-group/sh-solver-scheduler/pkg/options"
	"github.com/xuelang-group/sh-solver-scheduler/pkg/utils"
)

var (
	Config func(key string) gjson.Result
)

func SetupConfig() {
	Config = initConfig()
}

func initConfig() func(key string) gjson.Result {
	j := utils.TrimQuotes(GetEnvConfig().NodeConfig)
	if !gjson.Valid(j) {
		panic("invalid nodeconfig json")
	}

	return func(key string) gjson.Result {
		return gjson.Get(j, key)
	}
}

func GetSolverTaskCommand(context options.SolverTaskContext) string {
	b, _ := json.Marshal(context)
	contextEnc := base64.StdEncoding.EncodeToString(b)
	return fmt.Sprintf(`curl '%s/internal/sys/get/connect' -o /tmp/connect && chmod +x /tmp/connect && spentry=sh exec /tmp/connect %s`, "http://ossxxxxxxxxxxx", contextEnc)
}
